package com.xy.tianyu

import android.content.Intent
import android.view.View
import com.blankj.utilcode.util.FragmentUtils
import com.xy.tianyu.module.news.fragment.NewsFragment
import com.xy.tianyu.module.novel.BookActivity
import com.xy.tianyu.module.picture.activity.PictureActivity
import com.xy.tianyu.module.video.activity.VideoActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.system.exitProcess

class MainActivity : BaseActivity() {
    private lateinit var newsFragment: NewsFragment
    private lateinit var headView: View
    private var exitTime: Long = 0

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initView() {
        newsFragment = NewsFragment()

        FragmentUtils.add(supportFragmentManager,newsFragment,R.id.fr_container,false)

        initDrawerLayout()

    }

    private fun initDrawerLayout() {
        headView = navigation_draw.getHeaderView(0)

        navigation_draw.setNavigationItemSelectedListener { p0 ->
            when(p0.itemId){
                R.id.nav_menu_news ->{

                }

                R.id.nav_menu_video ->{
                    startActivity(Intent(MainActivity@this,VideoActivity::class.java))
                }

                R.id.nav_menu_novel ->{
                    startActivity(Intent(MainActivity@this,BookActivity::class.java))
                }

                R.id.nav_menu_picture ->{
                    startActivity(Intent(MainActivity@this,PictureActivity::class.java))
                }
            }
            drawer_main.closeDrawers()
            true
        }

    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000){
            exitTime = System.currentTimeMillis()
        }else{
            finish()
            exitProcess(0)
        }
    }

}